package cn.lijiajia3515.cairo.security.oauth2.jwt;

import cn.lijiajia3515.cairo.domain.CairoAccount;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.jwt.Jwt;

@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CairoJwtPrincipal extends Jwt {

	@Getter
	@Setter
	private CairoAccount account;

	/**
	 * @param jwt jwt
	 */
	public CairoJwtPrincipal(Jwt jwt) {
		super(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getHeaders(), jwt.getClaims());
		this.account = CairoAccount.builder().id(jwt.getSubject()).build();
	}

	/**
	 * @param jwt     jwt
	 * @param account user
	 */
	public CairoJwtPrincipal(Jwt jwt, CairoAccount account) {
		super(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getHeaders(), jwt.getClaims());
		this.account = account;
	}

	/**
	 * 获取 clientId
	 *
	 * @return clientId
	 */
	public String getClientId() {
		return super.getClaim(CairoJwtClaimNames.CLIENT_ID);
	}

	/**
	 * 获取 appId
	 *
	 * @return appId
	 */
	public String getAppId() {
		return super.getClaim(CairoJwtClaimNames.APP_ID);
	}

	/**
	 * 获取 appId
	 *
	 * @return appId
	 */
	public String getAzp() {
		return super.getClaim(CairoJwtClaimNames.AZP);
	}

	/**
	 * 授权类型
	 *
	 * @return 授权类型
	 */
	public String getGrantType() {
		return super.getClaim(OAuth2ParameterNames.GRANT_TYPE);
	}
}
